package com.example.keepserver.controller;

import com.example.keepserver.dto.ActivityDTO;
import com.example.keepserver.dto.PlatformStatsDTO;
import com.example.keepserver.dto.UserTypeDistributionDTO;
import com.example.keepserver.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activities")
@CrossOrigin(origins = "*") // 允许跨域请求，生产环境应指定具体域名
public class ActivityController {

    private final ActivityService activityService;

    @Autowired
    public ActivityController(ActivityService activityService) {
        this.activityService = activityService;
    }

    @GetMapping
    public ResponseEntity<List<ActivityDTO>> getActivities(
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        // 设置默认时间范围（如果未提供）
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        if (userId != null && !userId.isEmpty()) {
            return ResponseEntity.ok(activityService.getActivitiesByUserId(userId, startDate, endDate));
        } else {
            return ResponseEntity.ok(activityService.getAllActivities(startDate, endDate));
        }
    }

    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getActivityStats(
            @RequestParam("userId") String userId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getActivityStatsByUser(userId, startDate, endDate));
    }

    @GetMapping("/trends")
    public ResponseEntity<Map<String, Object>> getActivityTrends(
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getActivityTimeTrend(userId, startDate, endDate));
    }

    @GetMapping("/intensity")
    public ResponseEntity<Map<String, Object>> getTrainingIntensity(
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getTrainingIntensity(userId, startDate, endDate));
    }

    @GetMapping("/locations")
    public ResponseEntity<Map<String, Object>> getActivityLocations(
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getActivityLocations(userId, startDate, endDate));
    }

    @GetMapping("/type-distribution")
    public ResponseEntity<List<UserTypeDistributionDTO>> getUserTypeDistribution(
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getUserTypeDistribution(startDate, endDate));
    }

    @GetMapping("/platform-stats")
    public ResponseEntity<PlatformStatsDTO> getPlatformStats(
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        
        if (startDate == null) {
            startDate = LocalDateTime.now().minusDays(30);
        }
        if (endDate == null) {
            endDate = LocalDateTime.now();
        }
        
        return ResponseEntity.ok(activityService.getPlatformStats(startDate, endDate));
    }
} 